package com.tj.bo.integration.impl;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.utils.HttpUtil;
import com.tj.bo.common.conf.config.RedisConfigUtil;
import com.tj.bo.integration.MessageIntegrationService;
import com.tj.bo.integration.input.SingleSmsSendApInput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: anonymous
 * Date: 2020/06/11
 * Time: 下午2:16
 */
@Service
public class MessageIntegrationServiceImpl implements MessageIntegrationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageIntegrationServiceImpl.class);

    @Autowired
    private RedisConfigUtil configUtil;

    /**
     * 发邮件
     *
     * @param email
     * @param content
     * @param title
     * @return
     */
    @Override
    public boolean sendEmail(String email, String content, String title) {
        String url = configUtil.getMessageHost() + "/message/email/send";
        try {
            SingleSmsSendApInput singleSmsSendRequestRO = new SingleSmsSendApInput();
            LOGGER.info("发送邮件,email:{},content:{},title:{}", email, content, title);
            singleSmsSendRequestRO.setPhone(email);
            Map<String, String> replaceParam = new HashMap<>();
            singleSmsSendRequestRO.setBizCode("BINARY_OPTION_EMAIL_COMMON_CONTENT");
            singleSmsSendRequestRO.setTitle(title);
            replaceParam.put("content", content);
            singleSmsSendRequestRO.setReplaceParam(replaceParam);
            singleSmsSendRequestRO.setUserCode("");
            String s = JSON.toJSONString(singleSmsSendRequestRO);
            LOGGER.info("发送邮件,singleSmsSendRequestRO:{}", s);

            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            String resp = HttpUtil.postPageBody(url, headers, s);
            LOGGER.info("============== 发送邮件结果, resp:{} =============", resp);
            try {
                if (resp != null && JSON.parseObject(resp).getJSONObject("data").getBooleanValue("result")) {
                    return true;
                }
                return false;
            } catch (Exception e) {
                LOGGER.info("发送邮件解析返回参数失败,email:{},content:{},title:{}", email, content, title, e);
                return false;
            }
        } catch (Exception e) {
            LOGGER.info("发送邮件失败,email:{},content:{},title:{}", email, content, title);
            return false;
        }
    }

    /**
     * 发邮件
     *
     * @param userCode
     * @param content
     * @param title
     * @return
     */
    @Override
    public boolean sendEmailByUserCode(String userCode, String content, String title) {
        return false;
    }
}